package datastructure.graph;

/*
    图领接矩阵类
 */
public class MatGraphClass {
    final int MAXV = 100;
    final int INF = Integer.MAX_VALUE;
    int[][] edges;
    int n, e;
    String[] vexs;

    public MatGraphClass() {
        edges = new int[MAXV][MAXV];
        vexs = new String[MAXV];
    }

    public void createMatGraph(int[][] a, int n, int e) {
        this.n = n;
        this.e = e;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < e; j++) {
                edges[i][j] = a[i][j];
            }
        }
    }

    public void printMatGraph() {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < e; j++) {
                if (edges[i][j] == INF) {
                    System.out.print("∞ ");
                } else {
                    System.out.print(edges[i][j] + " ");
                }

            }
            System.out.println();
        }
    }

    /*
        求顶点的入度和出度
     */
    public int[] getDegree(int v) {
        int[] degree = {0, 0};
        for (int i = 0; i < n; i++) {
            if (edges[v][i] != INF && edges[v][i] != 0) {
                degree[0]++;
            }
        }

        for (int j = 0; j < n; j++) {
            if (edges[j][v] != INF && edges[j][v] != 0) {
                degree[1]++;
            }
        }
        return degree;
    }

    public static void main(String[] args) {
        int[][] data = {
                {0, 8, Integer.MAX_VALUE, 5, Integer.MAX_VALUE},
                {Integer.MAX_VALUE, 0, 3, Integer.MAX_VALUE, Integer.MAX_VALUE},
                {Integer.MAX_VALUE, Integer.MAX_VALUE, 0, Integer.MAX_VALUE, 6},
                {Integer.MAX_VALUE, Integer.MAX_VALUE, 9, 0, Integer.MAX_VALUE},
                {Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, 0}
        };

        MatGraphClass graph = new MatGraphClass();
        graph.createMatGraph(data, data.length, 5);
        graph.printMatGraph();
        for (int i : graph.getDegree(2)) {
            System.out.println(i);
        }

    }
}
